package com.kun.springsecurity.config;

import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author zhoukun
 * @Date 2021/2/15 18:55
 */
@Configuration
public class PageTurnConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/main").setViewName("main");
        registry.addViewController("/main2").setViewName("main2");
        registry.addViewController("/error").setViewName("error");
        registry.addViewController("/child/child1").setViewName("/child/child1");
        registry.addViewController("/child/child2").setViewName("/child/child2");
        registry.addViewController("/child/child3").setViewName("/child/child3");
        registry.addViewController("/show").setViewName("show");
    }

    /**
     * 自定义404页面
     * @return
     */
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        //java8 lambda写法
        return (factory -> {
//            ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
            ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
            factory.addErrorPages(errorPage404);
        });
    }

}
